package cn.xm.org.controller;

import cn.xm.basic.result.AjaxResult;
import cn.xm.org.domain.Emp;
import cn.xm.org.query.EmpQuery;
import cn.xm.basic.result.PageList;
import cn.xm.org.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(tags = "部门管理")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Emp> findAll() {
        return empService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "凭id查询")
    public Emp findById(@PathVariable("id") Long id) {
        return empService.findById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            empService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }

    @PutMapping("/save")
    @ApiOperation(value = "新增或修改")
    public AjaxResult addOrUpdate(@RequestBody Emp emp) {
        try {
            if (emp.getId() == null) {
                empService.add(emp);
            } else {
                empService.update(emp);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }

    @PostMapping("/query")
    @ApiOperation(value = "分页查询")
    public PageList<Emp> queryByPage(@RequestBody EmpQuery empQuery) {
        return empService.queryByPage(empQuery);

    }

    @PatchMapping("/batchdel")
    public AjaxResult batchdelete(@RequestBody List<Long> ids) {
        try {
            empService.batchDelete(ids);
            return new AjaxResult().me();
        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
            return new AjaxResult().me().fail(e.getMessage());
        }
    }


}
